package com.ddxz.middleground.master.service.impl;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.app.AppDetail;
import com.ddxz.middleground.master.dto.app.AppInfo;
import com.ddxz.middleground.master.dto.app.AppPage;
import com.ddxz.middleground.master.entity.AppEntity;
import com.ddxz.middleground.master.mapper.ApplicationMapper;
import com.ddxz.middleground.master.mapper.MenuMapper;
import com.ddxz.middleground.master.service.ApplicationService;
import com.ddxz.tool.core.common.CommonUtil;
import com.ddxz.tool.core.convert.BeanUtil;
import com.ddxz.tool.core.convert.DdxzUtil;
import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.ddxz.tool.web.dto.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import static com.ddxz.middleground.master.util.ErrorStatus.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/30
 */

@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Override
    public DdxzResponse getAppList(MasterHeader header, AppPage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<AppEntity> apps = appMapper.getAppList(page.getKeyword(), page.getStatus(), header.getTenant());
        return DdxzResponse.correct(PageResponse.getInstance(apps, AppDetail.class, DdxzUtil.SECOND_DATE));
    }

    @Override
    public DdxzResponse getAppDetail(MasterHeader header, String appId) {
        AppEntity entity = appMapper.getAppDetail(appId, header.getTenant());
        CommonUtil.ddxzAssetNull(entity, APP_NOT_EXISTS);
        return DdxzResponse.correct(BeanUtil.getTarget(entity, AppDetail.class, DdxzUtil.SECOND_DATETIME));
    }

    @Override
    public DdxzResponse appSave(MasterHeader header, AppInfo appInfo) {
        checkStatus(appInfo.getStatus());
        Integer num = appMapper.getNumByName(appInfo.getName(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, APP_NAME_REPEAT);
        num = appMapper.getNumByCode(appInfo.getCode(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, APP_CODE_REPEAT);
        AppEntity entity = new AppEntity();
        BeanUtil.copyProperties(appInfo, entity);
        if(StringUtil.isEmpty(appInfo.getId())) {
            // 新增应用
            entity.setId(CommonUtil.generatorId(T_APPLICATION));
            entity.setUserId(header.getUserId());
            entity.setNickname(header.getNickname());
            appMapper.saveApp(entity, header.getTenant());
        } else {
            appMapper.modifyApp(entity, header.getTenant());
        }
        return DdxzResponse.correct("保存应用成功", entity.getId());
    }

    @Override
    public DdxzResponse removeApp(MasterHeader header, String appId) {
        Integer num = menuMapper.getNumByAppId(appId, header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, APP_HAS_MENU);
        appMapper.removeApp(appId, header.getTenant());
        return DdxzResponse.correct(OK_MESSAGE);
    }

    private final ApplicationMapper appMapper;

    private final MenuMapper menuMapper;

    public ApplicationServiceImpl(ApplicationMapper appMapper, MenuMapper menuMapper) {
        this.appMapper = appMapper;
        this.menuMapper = menuMapper;
    }
}
